package com.hussein.android.quizgame;



import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hussein.android.quizgame.model.HighScore;
import com.hussein.android.quizgame.model.HighScoreType;
import com.hussein.android.quizgame.model.Record;


public class EndGameActivity extends Activity  {
	
	//keeps bonus score
	public static int bonus = 0;
	//keeps score of the user
	public static int score = 0;
	
	//keeps high score database connection
	public HighScore myHighScore =new HighScore(this, HighScoreType.POINTS, 10);

	//keeps the worstScore in the Highest Score database
	private int worstScore = 0;
	//true if the user deserve to enter High Score list; Otherwise false
	boolean isHighScore = false;
	
	//Screen Elements
	EditText edtName;
	Button btnOK;
	TextView txtScore;
	TextView txtHighestScore;
	TextView txtHighestScoreText;
	
	//Initialization of the screen
    @Override
    public void onCreate( Bundle savedInstanceState ){
    	TextView enterName;	
    	ArrayList<Record> records;
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.endgame);
        
        //get the highest score
        txtHighestScore = (TextView) findViewById(R.id.txtHighestScore);
        txtHighestScoreText = (TextView) findViewById(R.id.txtHighestScoreText);
        records = myHighScore.getRecords();
        
        //If the High Score list empty, it shows nothing
        //Otherwise, it shows the highest score
        if (records.size() > 0) {
        	txtHighestScore.setText(String.valueOf(records.get(0).getScore()));
        }
        else {
        	txtHighestScore.setVisibility(View.INVISIBLE);
        	txtHighestScoreText.setVisibility(View.INVISIBLE);
        }
        
        
        edtName = (EditText)findViewById(R.id.edtName);
        btnOK = (Button)findViewById(R.id.btnOk);
        enterName = (TextView)findViewById(R.id.txtEnterName);
        
        //show the current score 
        txtScore = (TextView) findViewById(R.id.txtScore);
        txtScore.setText(String.valueOf(score));
        
        //if player score is in the 10 highest scores
        if(records.size() < 10)
        	worstScore = 0;
        else
        	worstScore = records.get(records.size()-1).getScore();
        
        if(score > worstScore)
        {
        	edtName.setEnabled(true);
        	isHighScore = true;
        }
        else
        {
        	isHighScore = false;
        	edtName.setVisibility(View.INVISIBLE);
        	enterName.setVisibility(View.INVISIBLE);
        	edtName.setEnabled(false);
        }
        
        //OK button click events handling
        btnOK.setOnClickListener(new OnClickListener() {
        	
        	public void onClick( View v ){
        		
        		//if the score is high score, it sends the score to the highScore class
        		if (isHighScore) {
        			ScoresActivity.score = score;
        			ScoresActivity.user = edtName.getText().toString();
            		if(ScoresActivity.user.equalsIgnoreCase(""))
            			ScoresActivity.newScore = false;
            		else
            			ScoresActivity.newScore = true;
        		}
        		else {
        			ScoresActivity.newScore = false;
        		}
            	
        		//calls the highScore screen
            	Intent endGameIntent = new Intent(EndGameActivity.this, ScoresActivity.class);
            	startActivity(endGameIntent);
        	}
        });
    }
      
}
